/**
 * Title:       FIPAContent.java
 * Copyright: 
 * Copyright (c) 1996-2004 The Agent Factory Working Group. All rights reserved.
 * Licence:     This file is free software; you can redistribute it and/or modify
 *              it under the terms of the GNU Lesser General Public License as published by
 *              the Free Software Foundation; either version 2.1, or (at your option)
 *              any later version.
 *
 *              This file is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU Lesser General Public License for more details.
 *
 *              You should have received a copy of the GNU Lesser General Public License
 *              along with Agent Factory; see the file COPYING.  If not, write to
 *              the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *              Boston, MA 02111-1307, USA.
 */
package com.agentfactory.platform.mts;

import java.util.ArrayList;
/**
 * This class contains various methods that support the ALPHAAgent Factort
 * implementation of the FIPA Standards.
 *
 * @author  Rem Collier
 */
public class FIPAHelper {
    /**
     * This method tokenizes a FIPA Message String delimited by spaces that
     * are not wrapped in brackets.
     *
     * E.g. (example token (another token))
     *              ^     ^        ^
     *            Delimiters    Not a Delimiter
     *
     * @param message the FIPA message to be tokenized
     * @return an arraylist of tokens
     */
    public static ArrayList getMessageTokens(String message) {
        ArrayList tokens = new ArrayList();
        
        int length = message.length();
//        if (length == 0) return tokens;
        
        // Start by finding the opening bracket
        int index = 0;
        while ((message.charAt(index) != '(') && (index < length-1)) {
            index++;
        }
        int lastIndex = ++index;
        
        // now extract the "command" part (<commmand> [<type> <param> ]*)
        // this can be detected by either a space, colon, or open bracket
        boolean found = false;
        String tok = null;
        while (!found && (index < length)) {
            found = (message.charAt(index) == '(') ||
                    (message.charAt(index) == ':') ||
                    (message.charAt(index) == ' ');
            index++;
        }
        if (found) {
            tok = message.substring(lastIndex, index-1).trim();
            if (tok.length() > 0) {
                tokens.add(tok);
            }
            lastIndex = index-1;
            index = index-1;
            tok = null;
        }
        
        // now start work on the parameters
        int count = 0;
        boolean quotes = false;
        while ((count > -1) && (index < length)) {
            if (message.charAt(index) == '\"') {
                if (quotes) {
                    quotes = false;
                } else {
                    quotes = true;
                }
            } else if (message.charAt(index) == '(') {
                count++;
            } else if (message.charAt(index) == ')') {
                if ((count == 1) && !quotes) {
                    tok = message.substring(lastIndex, index+1).trim();
                    lastIndex = index+1;
                    count = 0;
                } else {
                    count--;
                }
            } else if ((message.charAt(index) == ' ') && (count == 0) && !quotes) {
                tok = message.substring(lastIndex, index).trim();
                lastIndex = index+1;
            }
          
            if (tok != null) {
                if (tok.length() > 0) {
                    tok = strip(tok, "\"");
                    tok = strip(tok, "\\");
                    tokens.add(tok);
                    tok = null;
                }
            }
            index++;
        }
        
        // Add the remaining token
        if ((lastIndex > -1) && (lastIndex < length)) {
            tok = message.substring(lastIndex, index-1).trim();
            if (tok.length() > 0) tokens.add(tok);
        }
     
        // What about if no tokens?
        if (tokens.size() == 0) {
            tokens.add(message);
        }
        
        return tokens;
    }

    public static byte[] toBytes(String text) {
        byte[] content = new byte[text.length()];
        for (int i = 0; i <text.length(); i++) {
            content[i] = (byte) text.charAt(i);
        }
        return content;
    }
    
    private static String strip(String text, String ch) {
        String temp = text;
        
        int index = temp.indexOf(ch);
        while (index >= 0) {
            temp = temp.substring(0, index) + temp.substring(index+1);
            index = temp.indexOf(ch);
        }
        
        return temp;
    }
}
